//
//  SetViewController.m
//  CivilianTravel
//
//  Created by xalo on 16/7/15.
//  Copyright © 2016年 许仕杰. All rights reserved.
//

#import "SetViewController.h"
#import "headimage.h"
#import "LoginViewController.h"
#import "SetlistViewController.h"
#import "PersonCenterViewController.h"
#import "OrderViewController.h"
#import "AccountViewController.h"
#import "CollectViewController.h"
#import "PhonoViewController.h"
#import "PassValue.h"
@interface SetViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableview;
@property(nonatomic,strong)UIView *headview;
@property(nonatomic,strong)NSArray *array;
@property(nonatomic,strong)UIImageView *imageview;

@property(nonatomic,strong)UISwitch *swich;
@property(nonatomic,strong)UIButton *btn;
@property(nonatomic,strong)UISlider *slider;
/** 头像大小 */
@property (nonatomic, assign) NSInteger boolInter;
@end

@implementation SetViewController
-(UIImageView *)imageview{
    if (!_imageview) {
        _imageview =[[UIImageView alloc]initWithFrame:CGRectMake(30, 30, 80, 80)];
        _imageview.layer.cornerRadius = 40;
        _imageview.layer.masksToBounds = YES;
        _imageview.userInteractionEnabled = YES;
    }return _imageview;
}
-(UITableView *)tableview{
    if (!_tableview) {
        _tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight-48) style:UITableViewStylePlain];
        _tableview.delegate =self;
        _tableview.dataSource = self;
        _tableview.showsVerticalScrollIndicator = NO;
        _tableview.separatorStyle = NO;
        _tableview.scrollEnabled = NO;
        
    }return _tableview;
}
-(UIView *)headview{
    if (!_headview) {
        _headview = [[UIView alloc]initWithFrame:CGRectMake(0,0, kWidth, kWidth/3)];
           }return _headview;
}
-(NSArray *)array{
    if (!_array) {
        _array  =[NSArray array];
        
    }return _array;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.boolInter = 1;
    UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight)];
    imageview.image=[UIImage imageNamed:@"set1.jpg"];
    [self.tableview addSubview:imageview];
    imageview.alpha = 0.5;
    self.navigationController.navigationBar.backgroundColor = [UIColor blueColor];
    UITapGestureRecognizer *singleTap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(action1:)];
    [self.imageview addGestureRecognizer:singleTap];
    singleTap.numberOfTapsRequired = 1;
    
    self.btn = [[UIButton alloc]initWithFrame:CGRectMake(150, 40, 100, 75)];
    [self.btn setTitle:@"登录/注册" forState:UIControlStateNormal];
    self.btn.contentHorizontalAlignment = 0;
    [self.btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.btn addTarget:self action:@selector(loginandregis) forControlEvents:UIControlEventTouchUpInside];
    
    self.array = @[@"1",@"5",@"3"];
    [self.headview addSubview:self.imageview];
    [self.headview addSubview:self.btn];
    //51 153 234
    
    
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.tableview.tableHeaderView = self.headview;
    [self.view addSubview:self.tableview];
    [self.tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    
    self.navigationController.navigationBarHidden = YES;

}


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if (![[PassValue sharePassValue] iconUrl]) {
        self.imageview.image =[UIImage imageNamed:@"水墨画.png"];
    } else {
        [self.imageview sd_setImageWithURL:[NSURL URLWithString:[[PassValue sharePassValue] iconUrl]]];
    }
    if ([[PassValue sharePassValue] username]) {
        [self.btn setTitle:[[PassValue sharePassValue] username] forState:UIControlStateNormal];
    }

    
    
}


-(void)action1:(id)sender{
    if (self.boolInter == 1) {
        [UIView animateWithDuration:0.2 animations:^{
            [self.imageview removeFromSuperview];
            self.imageview.frame = CGRectMake(0, 0, kWidth,kHeight+64);;
            self.imageview.layer.masksToBounds = NO;
            [self.view addSubview:self.imageview];
            
        } completion:^(BOOL finished) {
            self.tabBarController.tabBar.hidden = YES;
            
            self.boolInter = 0;
        }];
    } else {
        [UIView animateWithDuration:0.2 animations:^{
            
            [self.imageview removeFromSuperview];
            self.imageview.frame = CGRectMake(30, 30, 80, 80);
            self.imageview.layer.masksToBounds = YES;

            [self.view addSubview:self.imageview];
        } completion:^(BOOL finished) {
            self.tabBarController.tabBar.hidden = NO;
            self.boolInter = 1;
        }];
    }
    
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    if (indexPath.section==0&&indexPath.row ==0) {
            cell.textLabel.text = @"个人中心";
        cell.imageView.image =[UIImage imageNamed:@"个人中心1"];
    }
    else if (indexPath.section==0&&indexPath.row ==1){
        cell.textLabel.text =@"全部订单";
        cell.imageView.image=[UIImage imageNamed:@"钱包1"];
        
    }
    else if(indexPath.section==0&&indexPath.row ==2){
        cell.textLabel.text =@"账户";
        cell.imageView.image =[UIImage imageNamed:@"全部订单1.png"];
    }
    else if (indexPath.section ==1&&indexPath.row ==0){
        cell.textLabel.text = @"相册";
        cell.imageView.image =[UIImage imageNamed:@"相册1"];
    }
    else if(indexPath.section==1&&indexPath.row==1){
        cell.textLabel.text =@"收藏";
        cell.imageView.image =[UIImage imageNamed:@"收藏1"];
    }
    else if(indexPath.section==1&&indexPath.row==2){

        
        
        
        cell.textLabel.text =@"夜间模式";
        cell.imageView.image = [UIImage imageNamed:@"夜间模式-1"];
        self.swich = [[UISwitch alloc]initWithFrame:CGRectMake(kWidth -60, 5, 100, 50)];
        [cell addSubview:self.swich];
        BOOL setting =self.swich.isOn;

        [self.swich setOn:setting animated:YES];
        [self.swich addTarget:self action:@selector(actionswich:) forControlEvents:UIControlEventValueChanged];
        cell.accessoryType = UITableViewCellAccessoryNone;

       
    }
//    label.font = [UIFont fontWithName:@"Helvetica-Bold" size:10];
    else if (indexPath.section ==2&&indexPath.row ==0){
        cell.textLabel.text = @"清除缓存";
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
    }
    
    
    else if (indexPath.section==3&&indexPath.row==0) {
        cell.textLabel.text = @"免责声明";
        cell.textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:17];
//        cell.textLabel.textAlignment = NSTextAlignmentCenter;
    }
    else{
    
    cell.textLabel.text =self.array[indexPath.row];
    }
    
    return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 3;
    }else if (section ==1){
        return self.array.count;
    }else{
        return 1;
    }
    
}
//-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
//    return 10;
//}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0&&indexPath.row == 0) {
        if ([self.btn.titleLabel.text isEqual:@"登录/注册"]) {
            [self presentViewController:[[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil] animated:YES completion:nil];
        } else {
            PersonCenterViewController *pervc = [[PersonCenterViewController alloc]init];
            pervc.image1 =self.imageview.image;
            [self.navigationController pushViewController:pervc animated:YES];
        }
    }
    else if (indexPath.section ==0&&indexPath.row==1){
        if ([self.btn.titleLabel.text isEqual:@"登录/注册"]) {
            [self presentViewController:[[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil] animated:YES completion:nil];
        } else {
        OrderViewController *ordervc =[[OrderViewController alloc]init];
            [self.navigationController pushViewController:ordervc animated:YES];
        }
    }
    else if (indexPath.section==0&&indexPath.row==2){
        if ([self.btn.titleLabel.text isEqual:@"登录/注册"]) {
            [self presentViewController:[[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil] animated:YES completion:nil];
        } else {
        AccountViewController *accvc= [[AccountViewController alloc]init];
            [self.navigationController pushViewController:accvc animated:YES];
        }
    }else if (indexPath.section==1&&indexPath.row==0){
        if ([self.btn.titleLabel.text isEqual:@"登录/注册"]) {
            [self presentViewController:[[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil] animated:YES completion:nil];
        } else {
        PhonoViewController *photovc= [[PhonoViewController alloc]init];
        [self.navigationController pushViewController:photovc animated:YES];
        }
    }else if (indexPath.section==1&&indexPath.row==1){
        if ([self.btn.titleLabel.text isEqual:@"登录/注册"]) {
            [self presentViewController:[[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil] animated:YES completion:nil];
        } else {
        CollectViewController *collectvc =[[CollectViewController alloc]init];
        [self.navigationController pushViewController:collectvc animated:YES];
        }
    }else if(indexPath.section==1&&indexPath.row==2){
        
        
    }
   else if (indexPath.section==2&&indexPath.row ==0) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
        
        UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        UIAlertAction *clean = [UIAlertAction actionWithTitle:@"清除" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [UIView animateWithDuration:2 delay:2 options:UIViewAnimationOptionLayoutSubviews animations:^{
                
            } completion:^(BOOL finished) {
                
            }];
            
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"清除中····" message:@"" preferredStyle:UIAlertControllerStyleAlert];
            
            [self presentViewController:alert animated:YES completion:^{
                [self performSelector:@selector(removeAlertView:) withObject:alert afterDelay:5];
            }];
            
        }];
        
        [alert addAction:clean];
        [alert addAction:actionCancel];
        [self presentViewController:alert animated:YES completion:nil];
   }else if(indexPath.section==3&&indexPath.row ==0){
    SetlistViewController *setlistvc = [[SetlistViewController alloc]init];

    [self.navigationController pushViewController:setlistvc animated:YES];
}
}
- (void)removeAlertView:(UIAlertController*)alert
{
    
    //alertView消失
    [alert dismissViewControllerAnimated:YES completion:nil];
    
    
    UIAlertController *alert1 = [UIAlertController alertControllerWithTitle:@"清除成功" message:@"" preferredStyle:UIAlertControllerStyleAlert];
    
    [self presentViewController:alert1 animated:YES completion:^{
        [self performSelector:@selector(removeAlertView1:) withObject:alert1 afterDelay:0.8];
    }];
}
- (void)removeAlertView1:(UIAlertController*)alert1
{
    
    //alertView消失
    [alert1 dismissViewControllerAnimated:YES completion:nil];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


    
-(void)actionswich:(UISwitch *)sender{
    BOOL setting =self.swich.isOn;
    if ( setting) {
        NSLog(@"夜间模式");
        [[UIScreen mainScreen] setBrightness:0];
        
    }else{
        NSLog(@"白天模式");
         [[UIScreen mainScreen] setBrightness:100];
    }
}

-(void)loginandregis{
    
    if ([self.btn.titleLabel.text isEqual:@"登录/注册"]) {
        LoginViewController *logvc = [[LoginViewController alloc]init];
            __weak typeof (self)weakself=self;
            logvc.block = ^(NSString *string){
                [weakself.btn setTitle:string forState:UIControlStateNormal];
            };
    [self presentViewController:logvc animated:YES completion:nil];
    }
}
//-(void)sliderAction:(UISlider *)sender{
//    
//    [[UIScreen mainScreen] setBrightness:self.slider.value];
//    
//}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
